<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="优惠券类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择优惠券类型" clearable size="small">
          <el-option label="全场赠券" :value="0"/>
          <el-option label="会员赠券" :value="1"/>
          <el-option label="购物赠券" :value="2"/>
          <el-option label="注册赠券" :value="3"/>
        </el-select>
      </el-form-item>
      <el-form-item label="优惠卷名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入优惠卷名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="$router.push({ path: '/sms/couponAdd' })"
            v-hasPermi="['sms:coupon:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['sms:coupon:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['sms:coupon:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="couponList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="优惠卷名称" align="center" prop="name"/>
      <el-table-column label="优惠券类型" align="center" prop="type">
        <template slot-scope="scope">
          {{ type(scope.row.type) }}
        </template>
      </el-table-column>
      <el-table-column label="可使用商品" align="center" prop="useType">
        <template slot-scope="scope">
          {{ useType(scope.row.useType) }}
        </template>
      </el-table-column>
      <el-table-column label="使用门槛" align="center" prop="minPoint">
        <template slot-scope="scope">
          {{ '满' + scope.row.minPoint + '元可用' }}
        </template>
      </el-table-column>
      <el-table-column label="适用平台" align="center" prop="platform">
        <template slot-scope="scope">
          {{ platform(scope.row.platform) }}
        </template>
      </el-table-column>
      <el-table-column label="面值" align="center" prop="amount"/>
      <el-table-column label="有效期" align="center" width="180">
        <template slot-scope="scope">
          <span>{{
              parseTime(scope.row.startTime, '{y}-{m}-{d}') + '至' + parseTime(scope.row.startTime, '{y}-{m}-{d}')
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="$router.push({ path: '/sms/couponEdit', query: { id:scope.row.id } })"
              v-hasPermi="['sms:coupon:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['sms:coupon:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script>
import { listCoupon, getCoupon, delCoupon, addCoupon, updateCoupon, exportCoupon } from "@/api/sms/coupon";

export default {
  name: "Coupon",
  components: {},
  computed: {
    type() {
      return function (val) {
        if (val === 0) {
          return '全场赠券';
        } else if (val === 1) {
          return '会员赠券';
        } else if (val === 2) {
          return '购物赠券';
        } else if (val === 3) {
          return '注册赠券';
        }
      };
    },
    useType() {
      return function (val) {
        if (val === 0) {
          return '全场通用';
        } else if (val === 1) {
          return '指定分类';
        } else if (val === 2) {
          return '指定商品';
        }
      }
    },
    platform() {
      return function (val) {
        if (val === 0) {
          return '全平台';
        } else if (val === 1) {
          return '移动端';
        } else if (val === 2) {
          return '电脑端';
        }
      };
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券表格数据
      couponList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        name: null,
      }
    };
  },
  watch: {
    $route: {
      handler(to) {
        if (to.name !== 'Coupon') {
          return;
        }
        this.getList();
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    /** 查询优惠券列表 */
    getList() {
      this.loading = true;
      listCoupon(this.queryParams).then(response => {
        this.couponList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        name: null,
        platform: null,
        count: null,
        amount: null,
        perLimit: null,
        minPoint: null,
        startTime: null,
        endTime: null,
        useType: null,
        note: null,
        publishCount: null,
        useCount: null,
        receiveCount: null,
        enableTime: null,
        code: null,
        memberLevel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除优惠券编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delCoupon(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有优惠券数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportCoupon(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>